package com.jinly.ryder.flow.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinly.ryder.common.auth.LoginContent;
import com.jinly.ryder.flow.service.FlowApplyQueryService;
import com.jinly.ryder.flow.service.model.ApplyStatus;
import com.jinly.ryder.flow.service.model.AuditResult;
import com.jinly.ryder.flow.service.model.FlowApply;
import com.jinly.ryder.flow.service.model.FlowAudit;
import com.jinly.ryder.flow.web.mapping.FlowApplyVOMapping;
import com.jinly.ryder.flow.web.mapping.FlowAuditVOMapping;
import com.jinly.ryder.flow.web.vo.FlowApplyVO;
import com.jinly.ryder.flow.web.vo.FlowAuditVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("flow/query")
@RequiredArgsConstructor
public class FlowApplyQueryController {
    private final FlowApplyQueryService flowApplyQueryService;
    private final FlowApplyVOMapping flowApplyVOMapping;
    private final FlowAuditVOMapping flowAuditVOMapping;

    @GetMapping("userApplyPage")
    public Page<FlowApplyVO> userApplyPage(Long current, Long size, String flowName, String appName, ApplyStatus applyStatus) {
        FlowApply query = new FlowApply();
        query.setApplyStatus(applyStatus);
        query.setAppName(appName);
        query.setFlowName(flowName);
        query.setApplyUserId(LoginContent.getUserId());
        return flowApplyVOMapping.toVOPage(flowApplyQueryService.userApplyPage(new Page(current, size), query));
    }

    @GetMapping("userAuditPage")
    public Page<FlowApplyVO> userAuditPage(Long current, Long size, String flowName, String appName, AuditResult auditResult) {
        FlowApply applyQuery = new FlowApply();
        applyQuery.setAppName(appName);
        applyQuery.setFlowName(flowName);
        FlowAudit auditQuery = new FlowAudit();
        auditQuery.setAuditUserId(LoginContent.getUserId());
        auditQuery.setAuditResult(auditResult);
        Page<FlowAudit> page = flowApplyQueryService.userAuditPage(new Page(current, size), auditQuery, applyQuery);
        List<FlowApply> flowApplyList = flowApplyQueryService.batchQueryApply(page.getRecords().stream().map(FlowAudit::getApplyId).collect(Collectors.toSet()));
        Page resultPage = new Page<FlowApplyVO>(current, size, page.getTotal());
        List<FlowApplyVO> flowApplyVOList = page.getRecords().stream().map(audit -> {
            Optional<FlowApply> flowApply = flowApplyList.stream().filter(apply -> Objects.equals(audit.getApplyId(), apply.getId())).findFirst();
            if (flowApply.isPresent()) {
                FlowApplyVO flowApplyVO = flowApplyVOMapping.toVo(flowApply.get());
                flowApplyVO.setCurrFlowAudit(flowAuditVOMapping.toVo(audit));
                return flowApplyVO;
            }
            return null;
        }).filter(Objects::nonNull).collect(Collectors.toList());
        resultPage.setRecords(flowApplyVOList);
        return resultPage;
    }

    @GetMapping("apply/{applyId}")
    public FlowApplyVO applyDetail(@PathVariable("applyId") Long applyId) {
        FlowApplyVO flowApplyVO = flowApplyVOMapping.toVo(flowApplyQueryService.applyDetail(applyId));
        List<FlowAuditVO> auditList = flowAuditVOMapping.toVoList(flowApplyQueryService.auditList(applyId));
        flowApplyVO.setAuditList(auditList);
        return flowApplyVO;
    }
}
